#define _POSIX_C_SOURCE 200809L

#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>

int sig2str(int signo, char * str) {
    char * _str;
    char elli_str[6];
    if (signo == 0 || signo > 64)
        return -1;

    switch (signo) {
        case SIGHUP:
            _str= "HUP";
            break;
        case SIGINT:
            _str= "INT";
            break;
        case SIGQUIT:
            _str= "QUIT";
            break;
        case SIGILL:
            _str= "ILL";
            break;
        case SIGTRAP:
            _str= "TRAP";
            break;
        case SIGABRT:
            _str= "ABRT";
            break;
        case SIGBUS:
            _str= "BUS";
            break;
        default:
            sprintf(elli_str, "SIG%d", signo);
            _str= elli_str;
    }

    strcpy(str, _str);

    return 0;
}

int main(int argc, char * argv[]) {
    char * endptr;
    char sigstr[20];
    if (argc != 2) {
        fprintf(stderr, "Usage: a.out <signo>\n");
        exit(1);
    }
    int signo= strtol(argv[1], &endptr, 10);
    if (*endptr != 0) {
        fprintf(stderr, "signo should be integer!\n");
        exit(1);
    }

    if (sig2str(signo, sigstr) < 0) {
        printf("signo %d is invalid\n", signo);
        exit(0);
    }
    printf("signo %d, signame %s\n", signo, sigstr);
}
